package com.wuming.caiwu.service.impl;

import com.wuming.caiwu.core.BusinessException;
import com.wuming.caiwu.core.WumingErrorCode;
import com.wuming.caiwu.dao.MenuMapper;
import com.wuming.caiwu.dao.QsgBannerMapper;
import com.wuming.caiwu.model.Menu;
import com.wuming.caiwu.model.QsgBanner;
import com.wuming.caiwu.service.QsgBannerService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("qsgBannerService")
public class QsgBannerServiceImpl extends BaseServiceImpl implements QsgBannerService {

    @Autowired
    private QsgBannerMapper qsgBannerMapper;

    @Override
    public void saveBanner(QsgBanner banner) throws BusinessException {
        banner.setCreateDatetime(new Date());
        qsgBannerMapper.insert(banner);
    }

    @Override
    public void modifyBanner(QsgBanner banner) throws BusinessException {
        qsgBannerMapper.updateByPrimaryKey(banner);
    }

    @Override
    public QsgBanner getBannerById(Long id) throws BusinessException {
        return qsgBannerMapper.selectByPrimaryKey(id);
    }

    @Override
    public void removeBannerById(Long id) throws BusinessException {
        qsgBannerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<QsgBanner> getBannerByPage(int pageIndex, int pageSize) throws BusinessException {
        Map<Object, Object> maps = new HashMap<Object, Object>();
        try {
            return (List<QsgBanner>) this.getPageList(QsgBannerMapper.class, "selectByParams", maps, pageIndex, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }

    @Override
    public int getTotal() throws BusinessException {
        try {
            Map<Object, Object> maps = new HashMap<Object, Object>();
            return this.getTotal(QsgBannerMapper.class, "selectTotal", maps);
        } catch (Exception e) {
            logger.error("查询banner信息失败{}" + e.getMessage());
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }
}
